package com.sms.activity;





import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sms.database.datastore;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;


public class SentMessages extends ListActivity {
	public Context context=this;
	private datastore db;
	Cursor cursor = null;
	//private ArrayList<HashMap<String,String>> msg;
	private String[] msg;
	public void onCreate(Bundle savedInstanceState) {
		setTitle("Sent Message");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sentlist);
       
       // this.getListView().setDividerHeight(2);
       // fillData();
		db =new datastore(this);
		db.openDB();
		 msg = db.fetchAllMessages1();
		//List msgs = 
		db.close();
		
  
	/*	SimpleAdapter adapter = new SimpleAdapter(
				this,
				msg,
				R.layout.msg_row,
				new String[]{"phone","msg"},
				new int[]{R.id.text1,R.id.text2}
				
		);*/
		
		setListAdapter(new ArrayAdapter<String>(this, R.layout.msg_row, msg));
		ListView lv = getListView();
		  lv.setTextFilterEnabled(true);

		  lv.setOnItemClickListener(new OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View view,
		        int position, long id) {
		      // When clicked, show a toast with the TextView text
		      Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
		          Toast.LENGTH_SHORT).show();
		    }
		  });
		}
        
	 @Override
	    public boolean onCreateOptionsMenu(Menu menu) {

	        MenuInflater inflater = getMenuInflater();
	        inflater.inflate(R.layout.menu2, menu);
	        return true;
	    }
	 // Reaction to the menu selection
	    @Override
	    public boolean onMenuItemSelected(int featureId, MenuItem item) {

	        switch (item.getItemId()) {
	        case R.id.del:
	            deletesms();
	            return true;
	        case R.id.delete:
	        	delete_all();
	        	return true;
	        }
	        return super.onMenuItemSelected(featureId, item);
	    }

	    
	    private void delete_all() {
			// TODO Auto-generated method stub
	    	
	    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    	builder.setMessage("Are you sure you want to delete all Messages?")
	    	       .setCancelable(false)
	    	       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	    	           public void onClick(DialogInterface dialog, int id) {
	    	                //MyActivity.this.finish();
	    	        	   datastore db = new datastore(SentMessages.this);
	    	        	   db.openDB();
	    	        	   db.removeall();
	    	        	   db.close();
	    	        	   Intent i=new Intent(SentMessages.this,SentMessages.class);
	    	        	   startActivity(i); 
	    	        	   
	    	           }
	    	       })
	    	       .setNegativeButton("No", new DialogInterface.OnClickListener() {
	    	           public void onClick(DialogInterface dialog, int id) {
	    	                dialog.cancel();
	    	           }
	    	       });
	    	AlertDialog alert = builder.create();
	    	alert.show();
	    	
			
		}

		private void deletesms() {
		// TODO Auto-generated method stub
			Intent i = new Intent(SentMessages.this, Deletemessages.class);
	        startActivity(i); 
		
	}

		@Override
	    public boolean onOptionsItemSelected(MenuItem item) {

			 switch (item.getItemId()) {
		        case R.id.del:
		            deletesms();
		            return true;
		        case R.id.delete:
		        	delete_all();
		        	return true;
		        }
	        return super.onOptionsItemSelected(item);
	    }
	
	
	
		
	       
	    

	
	

}
